//
//  AddImageContentCell.m
//  MIDI
//
//  Created by 邵子凡 on 15/10/29.
//  Copyright © 2015年 QXSX. All rights reserved.
//

#import "AddImageContentCell.h"
#import "AppConfig.h"
#import "UIView+Extension.h"

@interface AddImageContentCell ()<UIScrollViewDelegate,UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (copy, nonatomic) NSString *placeHolder;
@property (strong, nonatomic) NSDictionary *dic;

@end

@implementation AddImageContentCell

//通过xib加载主控件
+(instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString *ID = @"AddImageContentCell";
    AddImageContentCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AddImageContentCell" owner:nil options:nil]firstObject] ;
        
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

- (void)awakeFromNib {
    
    //设置代理
    self.scrollView.delegate = self;
    //以分页的形式进行滚动
    self.scrollView.pagingEnabled = YES;
    //隐藏水平滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //添加滚动视图里面的子控件
    [self settingScrollView];
    
    self.placeHolder = @"输入你要表达的";
    
}

//添加滚动视图里面的子控件
- (void)settingScrollView{
    
    //循环创建一个大View
    for (int i=0; i<3; i++) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(kWindowWidth * i, 0, kWindowWidth, self.scrollView.height);
        //设置标记
        view.tag = i+1;
        //创建数字label
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.width * 0.08, self.scrollView.height * 0.08, 30, 20)];
        label.text = @"01";
        if (view.tag == 2) {
            label.text = @"02";
        }
        if (view.tag == 3) {
            label.text = @"03";
        }
        label.textColor = UIColorRGBA(219, 219, 219, 1);
        label.font = [UIFont systemFontOfSize:19];
        [view addSubview:label];
        //创建图片详情lable
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.width * 0.17, self.scrollView.height * 0.07, 45, 20)];
        label1.text = @"图片详情";
        label1.textColor = UIColorRGBA(219, 219, 219, 1);
        label1.font = [UIFont systemFontOfSize:10];
        [view addSubview:label1];
        //创建添加图片按钮
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(self.scrollView.width * 0.08, self.scrollView.height * 0.25, 97, 122.5)];
        [btn setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(addImage_Click:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
        btn.tag = i+1;
        
        //创建textView输入框
        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(self.scrollView.width * 0.4, self.scrollView.height * 0.25, 219, 122.5)];
        textView.tag = i+1;
        textView.delegate = self;
        textView.text = @"输入你要表达的:";
        textView.textColor = [UIColor blackColor];
        //设置边框颜色
        textView.layer.borderColor = UIColor.grayColor.CGColor;
        //设置边框粗细
        textView.layer.borderWidth = 1;
        [view addSubview:textView];
        
        [self.scrollView addSubview:view];
    }
    //设置滚动范围
    self.scrollView.contentSize = CGSizeMake(kWindowWidth * 3, 0);
    
}

//通过代理传按钮
- (void)addImage_Click:(id)sender{
    
    UIButton *bnt = (UIButton *)sender;
    
    if ([self.delegate respondsToSelector:@selector(uploadTextContent:Btn:)]) {
        
        [self.delegate uploadTextContent:self Btn:bnt];
        
    }
    
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    
    textView.text = @"";
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    
    if ([textView.text isEqualToString:@""]){
        textView.text = self.placeHolder;
    }
    
    //通过代理传textView内容
    if ([self.delegate respondsToSelector:@selector(uploadTextViewContent:)]) {
        [self.delegate uploadTextViewContent:textView];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
